/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mailbox.AddInviteData;
/*     */ import com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.RecurId;
/*     */ import com.zimbra.cs.mailbox.calendar.ZOrganizer;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Map;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AddCalendarItemInvite
/*     */   extends CalendarRequest
/*     */ {
/*     */   protected class AddInviteParser
/*     */     extends ParseMimeMessage.InviteParser
/*     */   {
/*     */     protected AddInviteParser() {}
/*     */     
/*     */     public ParseMimeMessage.InviteParserResult parseInviteElement(ZimbraSoapContext lc, OperationContext octxt, Account account, Element inviteElem)
/*     */       throws ServiceException
/*     */     {
/*  47 */       return CalendarUtils.parseInviteForAddInvite(account, AddCalendarItemInvite.this.getItemType(), inviteElem, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  53 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  54 */     Account acct = getRequestedAccount(zsc);
/*  55 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  56 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*  58 */     AddInviteParser parser = new AddInviteParser();
/*  59 */     Mailbox.SetCalendarItemData scid = SetCalendarItem.getSetCalendarItemData(zsc, octxt, acct, mbox, request, parser);
/*     */     
/*  61 */     Invite inv = scid.invite;
/*  62 */     CalendarItem calItem = mbox.getCalendarItemByUid(octxt, inv.getUid());
/*  63 */     int folderId = inv.isTodo() ? 15 : 10;
/*  64 */     if (calItem != null) {
/*  65 */       int f = calItem.getFolderId();
/*  66 */       if ((f != 3) && (f != 4)) {
/*  67 */         folderId = f;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  74 */     if ((!inv.hasOrganizer()) && (inv.hasOtherAttendees())) {
/*  75 */       if (scid.message == null) {
/*  76 */         ZimbraLog.calendar.info("Got malformed invite without organizer.  Clearing attendees to prevent inadvertent cancels.");
/*     */         
/*  78 */         inv.clearAttendees();
/*     */       } else {
/*  80 */         String fromEmail = scid.message.getSenderEmail(true);
/*  81 */         if (fromEmail != null) {
/*  82 */           boolean dangerousSender = false;
/*     */           String intendedForAddress;
/*     */           try
/*     */           {
/*  86 */             intendedForAddress = scid.message.getMimeMessage().getHeader("X-Zimbra-Calendar-Intended-For", null);
/*     */           }
/*     */           catch (MessagingException e) {
/*  89 */             throw ServiceException.FAILURE("error parsing message", e);
/*     */           }
/*  91 */           if ((intendedForAddress != null) && (intendedForAddress.length() > 0)) {
/*  92 */             if (intendedForAddress.equalsIgnoreCase(fromEmail)) {
/*  93 */               ZimbraLog.calendar.info("Got malformed invite without organizer.  Clearing attendees to prevent inadvertent cancels.");
/*     */               
/*  95 */               inv.clearAttendees();
/*  96 */               dangerousSender = true;
/*     */             }
/*  98 */           } else if (AccountUtil.addressMatchesAccount(acct, fromEmail)) {
/*  99 */             ZimbraLog.calendar.info("Got malformed invite without organizer.  Clearing attendees to prevent inadvertent cancels.");
/*     */             
/* 101 */             inv.clearAttendees();
/* 102 */             dangerousSender = true;
/*     */           }
/* 104 */           if (!dangerousSender) {
/* 105 */             ZOrganizer org = new ZOrganizer(fromEmail, null);
/* 106 */             String senderEmail = scid.message.getSenderEmail(false);
/* 107 */             if ((senderEmail != null) && (!senderEmail.equalsIgnoreCase(fromEmail)))
/* 108 */               org.setSentBy(senderEmail);
/* 109 */             inv.setOrganizer(org);
/* 110 */             ZimbraLog.calendar.info("Got malformed invite that lists attendees without specifying an organizer.  Defaulting organizer to: " + org.toString());
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 119 */     String calItemIdStr = calItem != null ? Integer.toString(calItem.getId()) : "(new)";
/* 120 */     if (!inv.hasRecurId()) {
/* 121 */       ZimbraLog.calendar.info("<AddCalendarItemInvite> id=%s, folderId=%d, subject=\"%s\", UID=%s", new Object[] { calItemIdStr, Integer.valueOf(folderId), inv.isPublic() ? inv.getName() : "(private)", inv.getUid() });
/*     */     }
/*     */     else
/*     */     {
/* 125 */       ZimbraLog.calendar.info("<AddCalendarItemInvite> id=%s, folderId=%d, subject=\"%s\", UID=%s, recurId=%s", new Object[] { calItemIdStr, Integer.valueOf(folderId), inv.isPublic() ? inv.getName() : "(private)", inv.getUid(), inv.getRecurId().getDtZ() });
/*     */     }
/*     */     
/*     */ 
/* 129 */     Element response = getResponseElement(zsc);
/* 130 */     if (calItem != null)
/*     */     {
/* 132 */       RecurId rid = scid.invite.getRecurId();
/* 133 */       Invite matchingInv = calItem.getInvite(rid);
/* 134 */       if ((matchingInv != null) && (matchingInv.isSameOrNewerVersion(scid.invite))) {
/* 135 */         response.addAttribute("calItemId", calItem.getId());
/* 136 */         response.addAttribute("invId", matchingInv.getMailItemId());
/* 137 */         response.addAttribute("compNum", matchingInv.getComponentNum());
/* 138 */         return response;
/*     */       }
/*     */     }
/*     */     
/* 142 */     Mailbox.AddInviteData aid = mbox.addInvite(octxt, inv, folderId, scid.message, false, false, true);
/* 143 */     if (aid != null) {
/* 144 */       calItem = mbox.getCalendarItemById(octxt, aid.calItemId);
/* 145 */       if (calItem != null) {
/* 146 */         Invite[] invs = calItem.getInvites(aid.invId);
/* 147 */         if ((invs != null) && (invs.length > 0)) {
/* 148 */           response.addAttribute("calItemId", aid.calItemId);
/* 149 */           response.addAttribute("invId", aid.invId);
/* 150 */           response.addAttribute("compNum", invs[0].getComponentNum());
/*     */         }
/*     */       }
/*     */     }
/* 154 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/AddCalendarItemInvite.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */